<!DOCTYPE html>
<html lang="zh-CN" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <title>密码</title>

    <!-- Bootstrap core CSS -->
    <script src="https://cdn.jsdelivr.net/npm/jquery@1.12.4/dist/jquery.min.js"></script>
    <link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">

    <!-- Custom styles for this template -->
    <!--<link href="signin.css" rel="stylesheet">-->
    <style type="text/css">
        body {
            padding-top: 140px;
            padding-bottom: 40px;
            background-color: #eee;
        }

        .form-signin {
            max-width: 330px;
            padding: 15px;
            margin: 0 auto;
        }

        .form-signin .form-signin-heading,
        .form-signin .checkbox {
            margin-bottom: 10px;
        }

        .form-signin .checkbox {
            font-weight: normal;
        }

        .form-signin .form-control {
            position: relative;
            height: auto;
            -webkit-box-sizing: border-box;
            -moz-box-sizing: border-box;
            box-sizing: border-box;
            padding: 10px;
            font-size: 16px;
        }

        .form-signin .form-control:focus {
            z-index: 2;
        }

        .form-signin input[type="email"] {
            margin-bottom: -1px;
            border-bottom-right-radius: 0;
            border-bottom-left-radius: 0;
        }

        .form-signin input[type="password"] {
            margin-bottom: 10px;
            border-top-left-radius: 0;
            border-top-right-radius: 0;
        }
    </style>

    <!--[if lt IE 9]>
    <script src="https://cdn.bootcss.com/html5shiv/3.7.3/html5shiv.min.js"></script>
    <script src="https://cdn.bootcss.com/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
</head>

<body>

<div class="container">

    <form class="form-signin" th:action="@{/update}" method="post">
        <h2 class="form-signin-heading" style="margin-bottom: 7px;display: inline">找回密码</h2>
        <a type="button" class="btn" th:href="@{/}"
           style="margin-bottom: 5px;margin-left: 65px">返回登录页面</a>
        <br>

        <!--th:if="${not #strings.isEmpty(msg)}"-->
        <span id="message" style="color: red"></span>
        <!--<span id="message" th:text="${msg}"></span>-->
        <input type="text" id="username" name="username" class="form-control" placeholder="请输入您原先的用户名" required
               autofocus minlength="5" maxlength="15" style="margin-bottom: 10px">
        <input type="password" id="password" name="password" class="form-control" placeholder="请输入您新的的密码" required
               onkeyup="value=value.replace(/[^\w\.\/]/ig,'')" minlength="5" maxlength="15" style="margin-bottom: 10px">
        <div class="form-group" style="float:left;display:flex;width:100%">
            <input type="email" id="email" name="email" class="form-control" placeholder="请输入您原先的邮箱" required>
            <input id="btnSendCode" style="float: right" type="button" class="btn btn-default" value="获取验证码"
                   onClick="sendMessage()"/>
        </div>
        <input type="code" id="code" name="code" class="form-control" placeholder="请输入验证码" required>
        <br>

        <button id="button" class="btn btn-lg btn-primary btn-block" type="submit">修改</button>
    </form>
</div> <!-- /container -->
<script>

    /*邮箱验证格式*/
    email.onkeydown = function () {
        var email = this.value;
        var reg = /^\w+@([a-z0-9]{1,6})(\.[a-z]{2,3})+$/i;
        if (reg.test(email)) {
            $("#message").text("邮箱格式正确").css("color", "green");
            $("#btnSendCode").removeAttr("disabled");
        } else {
            $("#message").text("邮箱格式错误").css("color", "red");
            $("#btnSendCode").attr("disabled", "true");
        }
    }

    /*发送验证码*/
    var count = 60; //间隔函数，1秒执行
    var InterValObj1; //timer变量，控制时间
    var curCount1;//当前剩余秒数

    /**
     * 校验用户名和邮箱
     */
    function sendMessage() {
        $.ajax({
            type: "post",
            url: "/verify?username=" + $("#username").val() + "&email=" + $("#email").val(),
            success(data) {
                if (data == "SUCCESS") {
                    console.log("校验成功 date = " + data)
                    $.ajax({
                        type: "post",
                        url: "/sendEmail2/" + $("#email").val(),
                    });
                    $("#message").text("邮件发送成功, 请注意查收").css("color", "green");
                } else if (data = "ERROR"){
                    $("#message").text("邮箱或密码填写错误").css("color", "red");
                    console.log("校验失败 date = " + data)
                }
            },error() {
                console.log("错误!")
            }
        });
        curCount1 = count;
        //设置button效果，开始计时
        $("#btnSendCode").attr("disabled", "true");
        $("#btnSendCode").val(+curCount1 + "秒再获取");
        InterValObj1 = window.setInterval(SetRemainTime1, 1000); //启动计时器，1秒执行一次
        //向后台发送处理数据
    }

    function SetRemainTime1() {
        if (curCount1 == 0) {
            window.clearInterval(InterValObj1);//停止计时器
            $("#btnSendCode").removeAttr("disabled");//启用按钮
            $("#btnSendCode").val("重新发送");
        } else {
            curCount1--;
            $("#btnSendCode").val(curCount1 + "秒再获取");
        }
    }
</script>
</body>

</html>